<?php

namespace app\admin\controller\mall\shop;

use app\common\controller\Backend;
use fast\Tree;
/**
 * 店铺分类
 *
 * @icon fa fa-circle-o
 */
class Cat extends Backend
{
    
    /**
     * Cats模型对象
     * @var \app\common\model\mall\shop\Cats
     */
    protected $model = null;
    protected $catList = null;
    protected $noNeedRight = ['selectpage'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\shop\Cat;
        $this->view->assign("statusList", $this->model->getStatusList());

        // 必须将结果集转换为数组
        $catList = collection($this->model->order('shop_id,pid,weigh', 'asc')->select())->toArray();
        //生成分类树
        Tree::instance()->init($catList);
        //生成权节点的字段为'simplename'
        $this->catlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
        //生成父节点数据，用于表单选择器
        $catdata = [0 => __('None')];
        foreach ($this->catlist as &$v)
        {
            if (!$v['ismenu']) continue;
            $catdata[$v['id']] = $v['name'];
        }
        $this->view->assign('catdata', $catdata);

    }
    
    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax())
        {

            $total = count($this->catlist);
            
            $result = array("total" => $total, "rows" => $this->catlist);
            
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * Selectpage搜索
     *
     * @internal
     */
    public function selectpage()
    {
        $this->request->filter(['strip_tags', 'htmlspecialchars']);
        $word = (array)$this->request->request("q_word/a");
        $page = $this->request->request("pageNumber");
        $pagesize = $this->request->request("pageSize");
        $andor = $this->request->request("andOr", "and", "strtoupper");
        $orderby = (array)$this->request->request("orderBy/a");
        $primarykey = $this->request->request("keyField",'id');
        $primaryvalue = $this->request->request("keyValue");
        $searchfield = (array)$this->request->request("searchField/a");
        $custom = (array)$this->request->request("custom/a");
        $order = $datalist = [];
        foreach ($orderby as $v) {
            $order[$v[0]] = $v[1];
        }
        //如果设置了主键值，则直接in
        if ($primaryvalue !== null) {
        	$where = [$primarykey => ['in', $primaryvalue]];
        } else {
        	$where = function ($query) use ($word, $andor, $searchfield, $custom) {
        		$logic = $andor == 'AND' ? '&' : '|';
        		$searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
        		foreach ($word as $k => $v) {
        			$query->where(str_replace(',', $logic, $searchfield), "like", "%{$v}%");
        		}
        		if ($custom && is_array($custom)) {
        			foreach ($custom as $k => $v) {
        				if (is_array($v) && 2 == count($v)) {
        					$query->where($k, trim($v[0]), $v[1]);
        				} else {
        					$query->where($k, '=', $v);
        				}
        			}
        		}
        	};
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
        	$this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        $total = $this->model->where($where)->where('ismenu','=', 1)->count();
        if ($total > 0) {
            $datalist = $this->model
                ->where($where)->where('ismenu','=', 1)
                ->field($this->selectpageFields)
                ->order($order)
                ->page($page, $pagesize)
                ->select();
        }
        return json(['list' => $datalist, 'total' => $total]);
    }

}
